<template>
  <el-dialog title="新規" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form size="small" :model="form" ref="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="品目" :label-width="formLabelWidth" :rules="[{ required: true, message: ' ' }]"
            prop="title">
            <el-input v-model="form.title" autocomplete="off" placeholder="品目"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品目コード" :label-width="formLabelWidth" :rules="[{ required: true, message: ' ' }]"
            prop="code">
            <el-input v-model="form.code" autocomplete="off" placeholder="品目コード"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="品目分類" :label-width="formLabelWidth" :rules="[{ required: true, message: ' ' }]"
            prop="class_id">
            <el-select v-model="form.class_id" placeholder="選択してください">
              <el-option v-for="item in classList" :key="item.id" :label="item.title" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="単価" :label-width="formLabelWidth" :rules="[{ required: true, message: ' ' }]"
            prop="price">
            <el-input v-model="form.price" autocomplete="off" placeholder="単価"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="単位" :label-width="formLabelWidth" prop="units">
            <el-input v-model="form.units" autocomplete="off" placeholder="単位"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品目分類" :label-width="formLabelWidth" prop="unit_id">
            <el-select v-model="form.unit_id" placeholder="選択してください">
              <el-option v-for="item in unitsList" :key="item.id" :label="item.title" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="品目詳細" :label-width="formLabelWidth" prop="content">
            <el-input type="textarea" v-model="form.content" autocomplete="off" placeholder="品目詳細"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="消費税" :label-width="formLabelWidth" prop="content">
            <el-radio-group v-model="form.tax">
              <el-radio :value="1">10%</el-radio>
              <el-radio :value="2">8%(軽減税率)</el-radio>
              <el-radio :value="3">8%</el-radio>
              <el-radio :value="4">5%</el-radio>
              <el-radio :value="5">免税</el-radio>
              <el-radio :value="6">非課税</el-radio>
              <el-radio :value="7">不課税</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="写真" :label-width="formLabelWidth" prop="images">
            <el-upload action="https://plusone-stock.com/api/uploads/uploadImg" list-type="picture-card"
              :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :on-success="handleSuccess">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogImgVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>
          <div style="margin-left: 120px;">
            <div>※アップロードできるファイル形式は、JPEG/PNGのみです。</div>
            <div>最大サイズ：5MB、比例：4:3</div>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">キャンセル</el-button>
        <el-button type="primary" @click="addClick()" :loading="loading">確定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import proclassApi from "@/api/proclass.js";
import unitsApi from "@/api/units.js";
import productApi from "@/api/product.js";
export default {
  data() {
    return {
      form: {
        tax: 1,
        imagesList: []
      },
      categoryList: [],
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      classList: [],
      unitsList: [],
      dialogImageUrl: '',
      dialogImgVisible: false,
    };
  },
  props: ["open_add"],
  created() {
    this.dialogVisible = this.open_add;
    this.getClassList();
    this.getUnitsList();
  },
  methods: {
    /*获取列表*/
    getClassList() {
      let self = this;
      let Params = {};
      proclassApi
        .lists(Params, true)
        .then((data) => {
          self.loading = false;
          self.classList = data.data.list;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*获取列表*/
    getUnitsList() {
      let self = this;
      let Params = {};
      unitsApi
        .lists(Params, true)
        .then((data) => {
          self.loading = false;
          self.unitsList = data.data.list;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*添加插件*/
    addClick() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          productApi
            .adds(params, true)
            .then((res) => {
              if (res.code == 1) {
                self.loading = false;
                ElMessage({
                  message: "追加成功",
                  type: "success",
                });
                self.dialogFormVisible(true);
              } else {
                self.loading = false;
              }
            })
            .catch((error) => { });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false,
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false,
        });
      }
    },
    handleRemove(file, fileList) {
      // const index = this.form.imagesList.indexOf(file.response.data);
      const index = 0;
      for (var i in this.form.imagesList) {
        if (this.form.imagesList[i].url == file.response.data) {
          index = i;
        }
      }
      this.form.imagesList.splice(index, 1);
      console.log(this.form.imagesList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogImgVisible = true;
    },
    handleSuccess(response, file, fileList) {
      if (response.code == 1) {
        this.form.imagesList.push({
          name: response.data,
          url: response.data
        });
      }
    },
  },
};
</script>

<style></style>
